<?php
namespace app\redbag\model;

use app\BaseModel;
use longbingcore\wxcore\WxPay;
use think\facade\Db;

class Invitation extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_redbag_invitation';


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 16:22
     * @功能说明:查看邀请数量
     */
    public function getCount($record_id){

        $dis = [

            'record_id'   => $record_id,
        ];

        $num = $this->where($dis)->count();

        return $num;
    }


    /**
     * @param $user_id
     * @param $p_id
     * @param $active_id
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-04-10 17:05
     */
    public function addInvitation($user_id,$c_id,$active_id,$uniacid){

        $record_model = new Record();

        $dis = [

            'user_id'   => $user_id,

            'active_id' => $active_id
        ];

        $record = $record_model->recordInfo($dis);


        //查看活动是否正在进行中
        if(empty($record)||$record['status']!=0){

            return true;
        }

        $count = $this->getCount($record['id']);

        //查看是否人满
        if($record['user_num']<=$count){

            return true;
        }

        $dis = [

            'uniacid'  => $uniacid,

            'user_id'  => $user_id,

            'c_id'     => $c_id,

            'active_id'=> $active_id,

            'record_id'=> $record['id']
        ];

        $data = $this->where($dis)->find();
        //没有就添加
        if(empty($data)){

            $this->add($dis);

        }

        return true;
    }


    /**
     * @param $data
     * @功能说明:添加
     * @author chenniang
     * @DataTime: 2020-04-10 17:10
     */
    public function add($data){

        $data['create_time'] = time();

        $res = $this->insert($data);

        return $res;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-04-10 18:36
     * @功能说明:
     */
    public function onCash($data){

        //获取用户openid
        $openid = Db::name('longbing_card_user')->where(['id'=>$data['user_id']])->value('openid');

        $data['money'] = round($data['price'],2);
        //微信相关模型
        $wx_pay = new WxPay($data['uniacid']);

        //微信提现
        $res    = $wx_pay->crteateMchPay($openid,$data['money']);

        return $res;

    }
















}